package com.project.decoration.controller.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationProject;
import com.project.decoration.domain.DecorationTask;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationProjectService;
import com.project.decoration.service.DecorationTaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 项目任务controller
 * @date 2025/02/17 10:12
 */
@Controller
@ResponseBody
@RequestMapping("task")
public class DecorationTaskController {

    @Autowired
    private DecorationTaskService decorationTaskService;
    @Autowired
    private DecorationProjectService decorationProjectService;

    /** 分页获取项目任务 */
    @PostMapping("getDecorationTaskPage")
    public Result getDecorationTaskPage(@RequestBody DecorationTask decorationTask) {
        Page<DecorationTask> page = new Page<>(decorationTask.getPageNumber(),decorationTask.getPageSize());
        QueryWrapper<DecorationTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(decorationTask.getProjectId()),DecorationTask::getProjectId,decorationTask.getProjectId())
                .eq(StringUtils.isNotBlank(decorationTask.getType()),DecorationTask::getType,decorationTask.getType())
                .eq(StringUtils.isNotBlank(decorationTask.getUserId()),DecorationTask::getUserId,decorationTask.getUserId())
                .like(StringUtils.isNotBlank(decorationTask.getRemark()),DecorationTask::getRemark,decorationTask.getRemark())
                .eq(decorationTask.getState() != null,DecorationTask::getState,decorationTask.getState());
        Page<DecorationTask> decorationTaskPage = decorationTaskService.page(page, queryWrapper);
        for (DecorationTask task : decorationTaskPage.getRecords()) {
            DecorationProject project = decorationProjectService.getById(task.getProjectId());
            task.setPriceName(project.getPriceName());
            task.setName(project.getName());
            task.setTel(project.getTel());
            task.setCity(project.getCity());
            task.setAddress(project.getAddress());
            task.setCommunity(project.getCommunity());
            task.setArea(project.getArea());
            task.setTypeHouse(project.getType());
            task.setStyle(project.getStyle());
            task.setIntroduce(project.getIntroduce());
        }
        return Result.success(decorationTaskPage);
    }

    /** 根据id获取项目任务 */
    @GetMapping("getDecorationTaskById")
    public Result getDecorationTaskById(@RequestParam("id")String id) {
        DecorationTask decorationTask = decorationTaskService.getById(id);
        return Result.success(decorationTask);
    }

    /** 保存项目任务 */
    @PostMapping("saveDecorationTask")
    public Result saveDecorationTask(@RequestBody DecorationTask decorationTask) {
        boolean save = decorationTaskService.save(decorationTask);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑项目任务 */
    @PostMapping("editDecorationTask")
    public Result editDecorationTask(@RequestBody DecorationTask decorationTask) {
        boolean save = decorationTaskService.updateById(decorationTask);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除项目任务 */
    @GetMapping("removeDecorationTask")
    public Result removeDecorationTask(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationTaskService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("项目任务id不能为空！");
        }
    }

}
